<?php

/**
 * Classe Form: permette di gestire in modo dinamico e personalizzabile un formulario
 *
 * @author Stefano Poncini
 */
/*
 * Gestione per i namespace ***non funzionante***
 * namespace php\module;
 */

class Form {

    private $method;
    private $action;
    private $data;

    /**
     * Costruttore della classe Form che valorizza le due variabili di classe method e action
     * 
     * @param string $method
     * @param string $action
     */
    function __construct($method, $action, $data="") {

        $this->method = $method;
        $this->action = $action;
        $this->data = $data;
    }

    /**
     * Metodo openForm: apre il tag <form>
     * 
     * @return string
     */
    public function openForm() {
        return '<form accept-charset="UTF-8" action="' . $this->action . '" method="' . $this->method . '" enctype="' . $this->data . '">';
    }

    /**
     * Metodo closeForm: chiude il tag </form>
     * 
     * @return string
     */
    public function closeForm() {
        return '</form>';
    }

    /**
     * Metodo createInput: crea un tag <input> in modo dinamico e personalizzato
     * 
     * @param string/int $id
     * @param string/int $name
     * @param string $type (text/password/button)
     * @param string $value
     * @return string
     */
    public function createInput($id, $name, $type = "text", $value = "") {
        return '<input class="form_element" id="' . $id . '" name="' . $name . '" type="' . $type . '" value="' . $value . '" /><br />';
    }

    /**
     * Metodo openSelect: apre il tag <select>
     * 
     * @param string $name
     * @return string
     */
    public function openSelect($name) {
        return '<select name="' . $name . '">';
    }

    /**
     * Metodo closeSelect: chiude il tag </select>
     * 
     * @return string
     */
    public function closeSelect() {
        return '</select>';
    }

    /**
     * Metodo createSpinner: crea dinamicamente tutti i tag <option> necessari per 
     * i giorni del mese, i mesi e gli anni
     * 
     * @param string $type
     * @param string $selected
     * @param int $start
     * @param int $num_elem
     * @return string
     */
    public function createSpinner($type, $selected = "", $start = null, $num_elem = null) {
        $options = "";
        switch ($type) {
            case 'day':
                for ($i = 1; $i <= 31; $i++) {
                    if ($selected == $i)
                        $options .= '<option value="' . $i . '" selected>' . $i . '</option>';
                    else
                        $options .= '<option value="' . $i . '">' . $i . '</option>';
                }
                break;
            case 'month':
                for ($i = 1; $i <= 12; $i++) {
                    if ($selected == $i)
                        $options .= '<option value="' . $i . '" selected>' . Util::getMonth($i) . '</option>';
                    else
                        $options .= '<option value="' . $i . '">' . Util::getMonth($i) . '</option>';
                }
                break;
            case 'year':
                for ($i = $start; $i >= ($start - $num_elem); $i--) {
                    if ($selected == $i)
                        $options .= '<option value="' . $i . '" selected>' . $i . '</option>';
                    else
                        $options .= '<option value="' . $i . '">' . $i . '</option>';
                }
                break;
        }

        return $options;
    }

    /**
     * Metodo createOption: permette di creare in modo personalizzato un tag <option>
     * 
     * @param string/int $value
     * @param string $text
     * @param string $selected
     * @return string
     */
    public function createOption($value, $text, $selected = "") {
        return '<option value="' . $value . '" ' . $selected . '>' . $text . '</option>';
    }

    /**
     * Metodo createLabel: crea semplicemente una stringa 
     * 
     * @param string $testo
     * @return string
     */
    public function createLabel($testo) {
        return $testo;
    }

    /**
     * Metodo createTextArea: crea un tag <textarea> in modo personalizzato e dinamico
     * @param int $row
     * @param int $cols
     * @param string $text
     * @return string
     */
    public function createTextArea($name, $row, $cols, $text="") {

        return '<textarea id="'. $name .'" name="'. $name .'" rows="' . $row . '" cols="' . $cols . '">' . $text . '</textarea>';
    }

    /**
     * Metodo validateField: serve per fare una verifica sui dati inseriti nei vari formulari.
     * Il metodo è statico perché è generico per ogni formulario e quindi non necessita di un istanza per 
     * essere richiamato
     * 
     * @static 
     * @param string $field
     * @param string $value
     * @return boolean
     */
    public static function validateField($field, $value, $user=null) {
        switch ($field) {
            case "name":
                if (empty($value) || !is_string($value) || strlen($value) < 3 || strlen($value) > 50) {
                    return false;
                } else {
                    return true;
                }
            case "surname":
                if (empty($value) || !is_string($value) || (strlen($value) < 3 || strlen($value) > 50)) {
                    return false;
                } else {
                    return true;
                }

            case "username":
                if (empty($value) || !is_string($value) || (strlen($value) < 3 || strlen($value) > 50)) {
                    return false;
                } else {
                    return true;
                }
            case "username_free":

                $sql = "SELECT count(*) as num_username from user where username = ?";

                $stmt = DB::getDBO()->prepare($sql);
                $stmt->execute(array($value));


                $result = $stmt->fetch();
                if ($result['num_username'] != 0)
                    return false;
                else
                    return true;
            case "email":
                if (!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $value) OR $value == "") {
                    return false;
                } else {
                    return true;
                }
            case "emailFree":
                $sql = "SELECT count(*) as num_email from user where email = ?";

                $stmt = DB::getDBO()->prepare($sql);
                $stmt->execute(array($value));


                $result = $stmt->fetch();
                if ($result['num_email'] != 0)
                    return false;
                else
                    return true;
            case "password":
                if ($value == "") {
                    return false;
                } else {
                    return true;
                }
                
            case "oldpw":
                $sql = "select count(*) as nr_res from user where password = md5(?) and username = ?";
 
                $stmt = DB::getDBO()->prepare($sql);
                $stmt->execute(array($value,$user->getUsername()));


                $result = $stmt->fetch();
                if ($result['nr_res'] != 0)
                    return false;
                else
                    return true;
        }
    }

}

?>
